<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/6-16:01
    // +----------------------------------------------------------------------
    // | Description:  微信粉丝管理
    // +----------------------------------------------------------------------


    namespace app\wechat\command;


    use think\console\Command;
    use app\wechat\service\WechatService;
    use think\console\Input;
    use think\console\Output;

    /**
     * Class Fans
     * @package app\wechat\command
     */
    class Fans extends Command
    {

        /**
         * @var array
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6-16:03 需要处理的模块
         */
        protected $module = ['list', 'tags', 'black'];

        /**
         * @param Input $input
         * @param Output $output
         * @return int|void|null
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: execute
         * @describe:执行指令
         */
        protected function execute(Input $input, Output $output)
        {
            foreach ($this->module as $m) {
                if (method_exists($this, $fun = "_{$m}")) {
                    $this->$fun();
                }
            }
        }


        /***
         * @param string $next
         * @param int $done
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: _list
         * @describe:同步微信粉丝列表
         */
        protected function _list($next = '', $done = 0)
        {
            $appid  = WechatService::getAppid();
            $wechat = WechatService::WeChatUser();
            $this->output->comment('开始同步微信粉丝数据 ...');
            while (!is_null($next) && is_array(
                    $result = $wechat->getUserList($next)
                ) && !empty($result['data']['openid'])) {
                foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
                    if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) {
                        foreach ($list['user_info_list'] as $user) {
                            $indexString = str_pad(++$done, strlen($result['total']), '0', STR_PAD_LEFT);
                            $this->output->writeln(
                                "({$indexString}/{$result['total']}) 正在更新粉丝 {$user['openid']} {$user['nickname']}"
                            );
                            \app\wechat\service\FansService::set($user, $appid);
                        }
                    }
                }
                $next = $result['total'] > $done ? $result['next_openid'] : null;
            }
            $this->output->comment('微信粉丝数据同步完成');
            $this->output->newLine();
        }

        /**
         * @param string $next
         * @param int $done
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: _black
         * @describe:同步粉丝黑名单列表
         */
        public function _black($next = '', $done = 0)
        {
            $wechat = WechatService::WeChatUser();
            $this->output->comment('开始同步微信黑名单数据 ...');
            while (!is_null($next) && is_array(
                    $result = $wechat->getBlackList($next)
                ) && !empty($result['data']['openid'])) {
                $done += $result['count'];
                foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
                    db("wechat_fans")->where(['is_black' => '0'])->whereIn('openid', $chunk)->update(
                        ['is_black' => '1']
                    );
                }
                $this->output->writeln("--> 共计同步微信黑名单{$result['total']}人");
                $next = $result['total'] > $done ? $result['next_openid'] : null;
            }
            $this->output->comment('微信黑名单数据同步完成');
            $this->output->newLine();
        }

        /**
         * @param int $index
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: _tags
         * @describe:同步粉丝标签列表
         */
        public function _tags($index = 0)
        {
            $appid  = WechatService::getAppid();
            $wechat = WechatService::WeChatTags();
            $this->output->comment('同步微信粉丝标签数据...');
            if (is_array($list = $wechat->getTags()) && !empty($list['tags'])) {
                $count = count($list['tags']);
                foreach ($list['tags'] as &$tag) {
                    $tag['appid'] = $appid;
                    $indexString  = str_pad(++$index, strlen($count), '0', STR_PAD_LEFT);
                    $this->output->writeln("({$indexString}/{$count}) 更新粉丝标签 {$tag['name']}");
                }
                db("wechat_fans_tags")->where(['appid' => $appid])->delete();
                db("wechat_fans_tags")->insertAll($list['tags']);
            }
            $this->output->comment('微信粉丝标签数据同步完成');
            $this->output->newLine();
        }


    }